home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.3 (Developer)…68k, x86, SPARC, PA-RISC] / NeXTSTEP 3.3 Dev Intel.iso / NextDeveloper / Headers / g++ / Complex.h < prev    next >
C/C++ Source or Header  |  1995-02-06  |  7KB  |  279 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1988 Free Software Foundation
  4.     written by Doug Lea (dl@rocky.oswego.edu)
  5.  
  6. This file is part of the GNU C++ Library.  This library is free
  7. software; you can redistribute it and/or modify it under the terms of
  8. the GNU Library General Public License as published by the Free
  9. Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.  This library is distributed in the hope
  11. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  12. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  13. PURPOSE.  See the GNU Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18.  
  19. #ifndef _Complex_h
  20. #ifdef __GNUG__
  21. #pragma interface
  22. #pragma cplusplus
  23. #endif
  24. #define _Complex_h 1
  25.  
  26.  
  27. #include <iostream.h>
  28. extern "C" {
  29. #include <math.h>
  30. }
  31. class Complex
  32. {
  33. #ifdef __ATT_complex__
  34. public:
  35. #else
  36. protected:
  37. #endif
  38.  
  39.   double           re;
  40.   double           im;
  41.  
  42. public:
  43.  
  44.   double           real() const;
  45.   double           imag() const;
  46.  
  47.                    Complex();
  48.                    Complex(const Complex& y);
  49.                    Complex(double r, double i=0);
  50.  
  51.                   ~Complex();
  52.  
  53.   Complex&         operator =  (const Complex& y);
  54.  
  55.   Complex&         operator += (const Complex& y);
  56.   Complex&         operator += (double y);
  57.   Complex&         operator -= (const Complex& y);
  58.   Complex&         operator -= (double y);
  59.   Complex&         operator *= (const Complex& y);
  60.   Complex&         operator *= (double y);
  61.  
  62.   Complex&         operator /= (const Complex& y); 
  63.   Complex&         operator /= (double y); 
  64.  
  65.   void             error(const char* msg) const;
  66. };
  67.  
  68.  
  69. // non-inline functions
  70.  
  71. Complex   operator /  (const Complex& x, const Complex& y);
  72. Complex   operator /  (const Complex& x, double y);
  73. Complex   operator /  (double   x, const Complex& y);
  74.  
  75. Complex   cos(const Complex& x);
  76. Complex   sin(const Complex& x);
  77.  
  78. Complex   cosh(const Complex& x);
  79. Complex   sinh(const Complex& x);
  80.  
  81. Complex   exp(const Complex& x);
  82. Complex   log(const Complex& x);
  83.  
  84. Complex   pow(const Complex& x, int p);
  85. Complex   pow(const Complex& x, const Complex& p);
  86. Complex   pow(const Complex& x, double y);
  87. Complex   sqrt(const Complex& x);
  88.    
  89. istream&  operator >> (istream& s, Complex& x);
  90. ostream&  operator << (ostream& s, const Complex& x);
  91.  
  92. // other functions defined as inlines
  93.  
  94. int  operator == (const Complex& x, const Complex& y);
  95. int  operator == (const Complex& x, double y);
  96. int  operator != (const Complex& x, const Complex& y);
  97. int  operator != (const Complex& x, double y);
  98.  
  99. Complex  operator - (const Complex& x);
  100. Complex  conj(const Complex& x);
  101. Complex  operator + (const Complex& x, const Complex& y);
  102. Complex  operator + (const Complex& x, double y);
  103. Complex  operator + (double x, const Complex& y);
  104. Complex  operator - (const Complex& x, const Complex& y);
  105. Complex  operator - (const Complex& x, double y);
  106. Complex  operator - (double x, const Complex& y);
  107. Complex  operator * (const Complex& x, const Complex& y);
  108. Complex  operator * (const Complex& x, double y);
  109. Complex  operator * (double x, const Complex& y);
  110.  
  111. double  real(const Complex& x);
  112. double  imag(const Complex& x);
  113. double  abs(const Complex& x);
  114. double  norm(const Complex& x);
  115. double  arg(const Complex& x);
  116.  
  117. Complex  polar(double r, double t = 0.0);
  118.  
  119.  
  120. // inline members
  121.  
  122. inline double  Complex::real() const { return re; }
  123. inline double  Complex::imag() const { return im; }
  124.  
  125. inline Complex::Complex() {}
  126. inline Complex::Complex(const Complex& y) :re(y.real()), im(y.imag()) {}
  127. inline Complex::Complex(double r, double i) :re(r), im(i) {}
  128.  
  129. inline Complex::~Complex() {}
  130.  
  131. inline Complex&  Complex::operator =  (const Complex& y) 
  132.   re = y.real(); im = y.imag(); return *this; 
  133.  
  134. inline Complex&  Complex::operator += (const Complex& y)
  135.   re += y.real();  im += y.imag(); return *this; 
  136. }
  137.  
  138. inline Complex&  Complex::operator += (double y)
  139.   re += y; return *this; 
  140. }
  141.  
  142. inline Complex&  Complex::operator -= (const Complex& y)
  143.   re -= y.real();  im -= y.imag(); return *this; 
  144. }
  145.  
  146. inline Complex&  Complex::operator -= (double y)
  147.   re -= y; return *this; 
  148. }
  149.  
  150. inline Complex&  Complex::operator *= (const Complex& y)
  151. {  
  152.   double r = re * y.real() - im * y.imag();
  153.   im = re * y.imag() + im * y.real(); 
  154.   re = r; 
  155.   return *this; 
  156. }
  157.  
  158. inline Complex&  Complex::operator *= (double y)
  159. {  
  160.   re *=  y; im *=  y; return *this; 
  161. }
  162.  
  163.  
  164. //  functions
  165.  
  166. inline int  operator == (const Complex& x, const Complex& y)
  167. {
  168.   return x.real() == y.real() && x.imag() == y.imag();
  169. }
  170.  
  171. inline int  operator == (const Complex& x, double y)
  172. {
  173.   return x.imag() == 0.0 && x.real() == y;
  174. }
  175.  
  176. inline int  operator != (const Complex& x, const Complex& y)
  177. {
  178.   return x.real() != y.real() || x.imag() != y.imag();
  179. }
  180.  
  181. inline int  operator != (const Complex& x, double y)
  182. {
  183.   return x.imag() != 0.0 || x.real() != y;
  184. }
  185.  
  186. inline Complex  operator - (const Complex& x)
  187. {
  188.   return Complex(-x.real(), -x.imag());
  189. }
  190.  
  191. inline Complex  conj(const Complex& x)
  192. {
  193.   return Complex(x.real(), -x.imag());
  194. }
  195.  
  196. inline Complex  operator + (const Complex& x, const Complex& y)
  197. {
  198.   return Complex(x.real() + y.real(), x.imag() + y.imag());
  199. }
  200.  
  201. inline Complex  operator + (const Complex& x, double y)
  202. {
  203.   return Complex(x.real() + y, x.imag());
  204. }
  205.  
  206. inline Complex  operator + (double x, const Complex& y)
  207. {
  208.   return Complex(x + y.real(), y.imag());
  209. }
  210.  
  211. inline Complex  operator - (const Complex& x, const Complex& y)
  212. {
  213.   return Complex(x.real() - y.real(), x.imag() - y.imag());
  214. }
  215.  
  216. inline Complex  operator - (const Complex& x, double y)
  217. {
  218.   return Complex(x.real() - y, x.imag());
  219. }
  220.  
  221. inline Complex  operator - (double x, const Complex& y)
  222. {
  223.   return Complex(x - y.real(), -y.imag());
  224. }
  225.  
  226. inline Complex  operator * (const Complex& x, const Complex& y)
  227. {
  228.   return Complex(x.real() * y.real() - x.imag() * y.imag(), 
  229.                  x.real() * y.imag() + x.imag() * y.real());
  230. }
  231.  
  232. inline Complex  operator * (const Complex& x, double y)
  233. {
  234.   return Complex(x.real() * y, x.imag() * y);
  235. }
  236.  
  237. inline Complex  operator * (double x, const Complex& y)
  238. {
  239.   return Complex(x * y.real(), x * y.imag());
  240. }
  241.  
  242. inline double  real(const Complex& x)
  243. {
  244.   return x.real();
  245. }
  246.  
  247. inline double  imag(const Complex& x)
  248. {
  249.   return x.imag();
  250. }
  251.  
  252. inline double  abs(const Complex& x)
  253. {
  254.   return hypot(x.real(), x.imag());
  255. }
  256.  
  257. inline double  norm(const Complex& x)
  258. {
  259.   return (x.real() * x.real() + x.imag() * x.imag());
  260. }
  261.  
  262. inline double  arg(const Complex& x)
  263. {
  264.   return atan2(x.imag(), x.real());
  265. }
  266.  
  267. inline Complex  polar(double r, double t)
  268. {
  269.   return Complex(r * cos(t), r * sin(t));
  270. }
  271.  
  272. #endif
  273.